package com.hand.hap.demo.service.impl;

import com.hand.hap.code.rule.exception.CodeRuleException;
import com.hand.hap.code.rule.service.ISysCodeRuleProcessService;
import com.hand.hap.core.IRequest;
import com.hand.hap.demo.dto.DemoBankBranch;
import com.hand.hap.demo.service.IDemoBankBranchService;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hand.hap.demo.dto.DemoBank;
import com.hand.hap.demo.service.IDemoBankService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class DemoBankServiceImpl extends BaseServiceImpl<DemoBank> implements IDemoBankService {

    @Autowired
    private IDemoBankBranchService demoBankBranchService;

    @Autowired
    private IDemoBankService demoBankService;

    @Autowired
    ISysCodeRuleProcessService codeRuleProcessService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int myBatchDelect(IRequest request, List<DemoBank> list) throws Exception {

        int count = 0;
        if (list == null || list.isEmpty()) {
            throw new Exception("缺失数据");
        }

        for (int i = 0; i < list.size(); i++) {
            DemoBank demoBank = new DemoBank();
            Long bankId = demoBank.getBankId();
            //查询头ID对应的行
            DemoBankBranch demoBankBranchSelect = new DemoBankBranch();
            demoBankBranchSelect.setBankId(bankId);
            List<DemoBankBranch> bankBranchList = demoBankBranchService.select(request, demoBankBranchSelect, 1, 0);

            if (bankBranchList != null || !bankBranchList.isEmpty()) {

                //删除行
                demoBankBranchService.batchDelete(bankBranchList);
            }

            //删除头
            int n = demoBankService.deleteByPrimaryKey(demoBank);

            count += n;
        }

        return count;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DemoBank> myBatchSubmit(IRequest request, List<DemoBank> list) {

        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {

                DemoBank demoBank = list.get(i);
                Long bankId = demoBank.getBankId();
                if (bankId == null||bankId==0) {

                    demoBankService.insertSelective(request,demoBank);
                    bankId=demoBank.getBankId();
                    List<DemoBankBranch> demoBankBranchList = demoBank.getDemoBankBranchList();

                    if (demoBankBranchList!=null &&!demoBankBranchList.isEmpty()){
                        for (int j = 0; j < demoBankBranchList.size(); j++) {
                            demoBankBranchList.get(j).setBankId(bankId);
                        }
                    }

                    demoBankBranchService.myBatchSubmit(request,demoBankBranchList);
                } else {

                    demoBankService.updateByPrimaryKeySelective(request,demoBank);
                    List<DemoBankBranch> demoBankBranchList = demoBank.getDemoBankBranchList();

                    if (demoBankBranchList!=null &&!demoBankBranchList.isEmpty()){
                        for (int j = 0; j < demoBankBranchList.size(); j++) {
                            if (demoBankBranchList.get(j).getBankId()==null){

                                demoBankBranchList.get(j).setBankId(bankId);
                            }
                        }
                    }
                    demoBankBranchService.myBatchSubmit(request,demoBankBranchList);
                }
            }
        }

        return list;

    }
}